<?php


namespace app\system\service;

class NewsService
{
	public static function getlist($options)
	{
		$where = [];
		if (isset($options["cid"]) && $options["cid"] != "") {
			$where[] = ["cid", "=", $options["cid"]];
		}
		if (isset($options["title"]) && $options["title"] != "") {
			$where[] = ["title", "like", "%" . $options["title"] . "%"];
		}
		$list = \think\Db::name("news")->where($where)->order(["sort" => "desc", "id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("news")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_array_info($item)
	{
		return $item;
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("news")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_array_info($item)];
	}
	public static function add($options)
	{
		\think\Db::startTrans();
		try {
			$news_classify = \think\Db::name("news_classify")->where(["id" => $options["cid"]])->find();
			if (empty($news_classify)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类ID不存在"];
			}
			\think\Db::name("news")->insert(["cid" => $options["cid"], "title" => $options["title"], "content" => $options["content"], "sort" => $options["sort"], "type" => $options["type"], "addtime" => time()]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("news")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$news_classify = \think\Db::name("news_classify")->where(["id" => $options["cid"]])->find();
			if (empty($news_classify)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类ID不存在"];
			}
			$data = ["cid" => $options["cid"], "title" => $options["title"], "content" => $options["content"], "sort" => $options["sort"], "type" => $options["type"]];
			\think\Db::name("news")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("news")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("news")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_classify_list($options)
	{
		$where = [];
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%" . $options["name"] . "%"];
		}
		$count = \think\Db::name("news_classify")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("news_classify")->where($where)->order("sort", "desc")->page($options["page"], $options["limit"])->select();
		$data = [];
		foreach ($list as $item) {
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_classify_info($options)
	{
		$data = \think\Db::name("news_classify")->where(["id" => $options["id"]])->find();
		if (empty($data)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function add_classify($options)
	{
		\think\Db::startTrans();
		try {
			$data = ["name" => $options["name"], "sort" => $options["sort"], "type" => $options["type"], "addtime" => time()];
			$insert = \think\Db::name("news_classify")->insert($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit_classify($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("news_classify")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
			}
			$data = ["name" => $options["name"], "sort" => $options["sort"], "type" => $options["type"]];
			$update = \think\Db::name("news_classify")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_classify($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("news_classify")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
			}
			$del = \think\Db::name("news_classify")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}